﻿#include"head.h"
#include <iostream>
#include <vector>
#include <utility>
#include <stdlib.h>
#include <time.h>
#include <fstream>
#include <map>
#pragma warning(disable:4996)

int main(int argc, char* argv[])
{
    srand((unsigned)time(NULL));
    Sudoku sudo;
    vector<int> range;
    int gameNum;
    int gameLevel = 0;
    bool mustUnique = false;  //解是否必须唯一
    int isN = 0;
    vector<Board> boards;
    ofstream outfile;
    // 只应该有3、4、5个参数
    if (argc != 3 && argc != 4 && argc != 5)
    {
        cout << "参数错误" << endl;
        return -1;
    }
    else if (argc == 3) // 对应-c,-s,单独-n
    {
        if (!strcmp(argv[1], "-c")) // -c
        {
            if (atoi(argv[2]) <= 0 || atoi(argv[2]) > 1000000)
            {
                cout << "参数错误" << endl;
                return -1;
            }
            else
            {
                outfile.open("sudoku_final.txt", ios::out | ios::trunc);
                range.push_back(0);
                generateGame(atoi(argv[2]), range, outfile, sudo, mustUnique);
                range.clear();
                outfile.close();
            }
        }
        else if (!strcmp(argv[1], "-s"))
        {
            ifstream f(argv[2]);
            if (!f.good())
            {
                cout << "参数错误" << endl;
                return -1;
            }
            outfile.open("sudoku.txt", ios::out | ios::trunc);
            boards = readFile(argv[2]);
            for (size_t i = 0; i < boards.size(); i++)
            {
                vector<Board> result = sudo.solveSudoku(boards[i]);
                writeFile(result, outfile);
            }
            outfile.close();
        }
        else if (!strcmp(argv[1], "-n"))
        {
            if (atoi(argv[2]) <= 0 || atoi(argv[2]) > 10000)
            {
                cout << "参数错误" << endl;
                return -1;
            }
            else
            {
                gameNum = atoi(argv[2]);
                isN = 1;
            }
        }
        else
        {
            cout << "参数错误" << endl;
            return -1;
        }
    }
    else if (argc == 4) // 对应-n -u
    {
        if (!strcmp(argv[1], "-n") && !strcmp(argv[3], "-u"))
        {
            if (atoi(argv[2]) <= 0 || atoi(argv[2]) > 10000)
            {
                cout << "参数错误" << endl;
                return -1;
            }
            else
            {
                gameNum = atoi(argv[2]);
                mustUnique = true;
                isN = 1;
            }
        }
        else
        {
            cout << "参数错误" << endl;
            return -1;
        }
    }
    else if (argc == 5) // 对应-n -m或-n -r
    {
        if (!strcmp(argv[1], "-n"))
        {
            if (atoi(argv[2]) <= 0 || atoi(argv[2]) > 10000)
            {
                cout << "参数错误" << endl;
                return -1;
            }
            else
            {
                if (!strcmp(argv[3], "-m"))
                {
                    if (atoi(argv[4]) == 1 || atoi(argv[4]) == 2 || atoi(argv[4]) == 3)
                    {
                        gameNum = atoi(argv[2]);
                        gameLevel = atoi(argv[4]);
                        isN = 1;
                    }
                    else
                    {
                        cout << "参数错误" << endl;
                        return -1;
                    }
                }
                else if (!strcmp(argv[3], "-r"))
                {
                    char* p;
                    p = strtok(argv[4], "~");
                    while (p)
                    {
                        range.push_back(atoi(p));
                        p = strtok(NULL, "~");
                    }
                    if (range.size() != 2)
                    {
                        cout << "参数错误" << endl;
                        return -1;
                    }
                    if ((range[0] >= range[1]) || range[0] < 20 || range[1] > 55)
                    {
                        cout << "参数错误" << endl;
                        return -1;
                    }
                    gameNum = atoi(argv[2]);
                    isN = 1;
                }
                else
                {
                    cout << "参数错误" << endl;
                    return -1;
                }
            }
        }
        else
        {
            cout << "参数错误" << endl;
            return -1;
        }
    }
    if (isN) // 是N
    {
        if (range.empty()) // 非-r
        {
            if (gameLevel == 1)
            {
                range.push_back(20);
                range.push_back(30);
            }
            else if (gameLevel == 2)
            {
                range.push_back(30);
                range.push_back(40);
            }
            else if (gameLevel == 3)
            {
                range.push_back(40);
                range.push_back(55);
            }
            else
            {
                range.push_back(20);
                range.push_back(55);
            }
        }
        outfile.open("sudoku_game.txt", ios::out | ios::trunc);
        generateGame(gameNum, range, outfile, sudo, mustUnique);
        range.clear();
        outfile.close();
    }
    return 0;
}
